/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.flowumi.controladores.admin;

import com.flowumi.controladores.SessionFlowumiUtilBean;
import com.flowumi.dto.Curso;
import com.flowumi.dto.Usuario;
import com.flowumi.facade.AdminFacade;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;

/**
 *
 * @author ideacentre
 */
@ManagedBean
@ViewScoped
public class AdminDetalleCursoBean {

    private static final org.apache.log4j.Logger LOGGER =
            org.apache.log4j.Logger.getLogger(AdminDetalleCursoBean.class);
    
    @EJB
    AdminFacade adminFacade;
    @ManagedProperty("#{sessionFlowumiUtil}")
    private SessionFlowumiUtilBean sessionFlowumiUtilBean;
    private Curso curso;
    private Curso cursoEdit;
    
    /**
     * Creates a new instance of AdminDetalleCursoBean
     */
    public AdminDetalleCursoBean() {
    }
    
    @PostConstruct
    public void init(){
        curso = adminFacade.getDetalleCurso(sessionFlowumiUtilBean.getCodigoAdminCursoDetalle());
        cursoEdit = curso;
    }

    public void setSessionFlowumiUtilBean(SessionFlowumiUtilBean sessionFlowumiUtilBean) {
        this.sessionFlowumiUtilBean = sessionFlowumiUtilBean;
    }

    public Curso getCurso() {
        return curso;
    }

    public void setCurso(Curso curso) {
        this.curso = curso;
    }

    public Curso getCursoEdit() {
        return cursoEdit;
    }

    public void setCursoEdit(Curso cursoEdit) {
        this.cursoEdit = cursoEdit;
    }
    
    
    public List<Usuario> getListaEstudiantesNoCurso(){
        return adminFacade.getListaEstudiantesNoCurso(curso.getIdCurso());
    }
    
    public Date getFechaActual(){
        return new Date();
    }
    
    public Date getFechaMesDespuesFechaInicial() {
        if (cursoEdit.getFechaInicio() == null) {
            return new Date();
        }
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(cursoEdit.getFechaInicio().getTime());
        c.add(Calendar.MONTH, 1);
        return c.getTime();
    }
    
    public void Actualizarcurso(){
        curso = adminFacade.ActualizarCurso(cursoEdit);
    }
    
    public void AgregarEstudiante(String codigo){
        curso = adminFacade.AgregarEstudiante(curso, codigo);
    }
    
    public void SacarEstudiante(String codigo){
        curso = adminFacade.SacarEstudiante(curso, codigo);
    }
    
    public void handleDateSelect() {
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(cursoEdit.getFechaInicio().getTime());
        c.add(Calendar.MONTH, 1);
        if (cursoEdit.getFechaFinalizacion().before(c.getTime())) {
            cursoEdit.setFechaFinalizacion(c.getTime());
        }
    }
}
